<?php
	require("libraries/student.php");
	class Restaurants extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_restaurant");
			$this->load->helper("form");
			$this->load->library("form_validation");
			$this->load->library("string");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/restaurants/index/';
			$config['total_rows'] = $this->model_restaurant->count_all();
			$data['title'] = "Manage restaurants";
			$data['template'] = "restaurants/list_restaur";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 0;
			$data['listcate']= $this->model_restaurant->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function active(){
			$id   	= $this->uri->segment(4);
			$status	= $this->uri->segment(5);
			$db = array(
				"restaurant_status" => $status 
			);
			$this->model_restaurant->update_status($db,$id);
			redirect(base_url()."admin/restaurants");
		}
		public function add(){
		  	$data['title'] = "Add restaurant";
			$data['template'] = "restaurants/add_restaur";
			$data['data'] = "";
			$data['act'] = 0;
			$data['category'] = $this->model_restaurant->category();
			if($this->input->post("ok") != ""){
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('restaurant_title'))));
				$value = array(
						"restaurant_title" => $this->input->post("restaurant_title"),
						"restaurant_rewrite" => $url,
						"restaurant_star" => $this->input->post("restaurant_star"),
						"restaurant_order" => $this->input->post("restaurant_order"),
						"restaurant_info" => $this->input->post("restaurant_info"),
						"restaurant_full" => $this->input->post("restaurant_full"),
						"cate_id" => $this->input->post("cate_id")
				);
				if($_FILES['restaurant_img']['name'] != NULL){
					$config['upload_path']   = './uploads/restaurants';
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']	= '1500';
					$config['max_width']  = '1500';
					$config['max_height']  = '1500';
					$config['encrypt_name'] = TRUE;
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("restaurant_img")){
						$data = array('error' => $this->upload->display_errors());
						$data['title'] = "Add restaurant";
						$data['template'] = "restaurants/add_restaur";
						$data['data'] = "";
						$data['act'] = 0;
						$data['category'] = $this->model_restaurant->category();
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$value['restaurant_image'] = $data['file_name'];
						$this->createThumbnail($value['restaurant_image'],"restaurants",160,160);
					}
				}
				$this->model_restaurant->add($value);
				redirect(base_url()."admin/restaurants");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['title']= "Edit restaurant";
			$data['template'] = "restaurants/edit_restaur";
			$data['data'] = "";
			$data['act'] = 0;
			$data['getdata'] = $this->model_restaurant->getdata($id);
			$data['category'] = $this->model_restaurant->category();
			if($data['getdata'] == NULL){ die("This restaurant is not availabel");}
			$data['stt'] = $data['getdata']['cate_id'];
			if($this->input->post("ok") != ""){
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('restaurant_title'))));
				$value = array(
					"restaurant_title" => $this->input->post("restaurant_title"),
					"restaurant_rewrite" => $url,
					"restaurant_star" => $this->input->post("restaurant_star"),
					"restaurant_order" => $this->input->post("restaurant_order"),
					"restaurant_info" => $this->input->post("restaurant_info"),
					"restaurant_full" => $this->input->post("restaurant_full"),
					"cate_id" => $this->input->post("cate_id")
				);
				if($_FILES['restaurant_img']['name'] != NULL){
					$config['upload_path']   = './uploads/restaurants';
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']	= '1500';
					$config['max_width']  = '1500';
					$config['max_height']  = '1500';
					$config['encrypt_name'] = TRUE;
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("restaurant_img")){
						$data = array('error' => $this->upload->display_errors());
						$data['title']= "Edit restaurant";
						$data['template'] = "restaurants/edit_restaur";
						$data['data'] = "";
						$data['act'] = 0;
						$data['getdata'] = $this->model_restaurant->getdata($id);
						$data['category'] = $this->model_restaurant->category();
						$data['stt'] = $data['getdata']['cate_id'];
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$value['restaurant_image'] = $data['file_name'];
						$this->createThumbnail($value['restaurant_image'],"restaurants",160,160);
					}
				}
				$this->model_restaurant->update($value,$id);
				redirect(base_url()."admin/restaurants/index");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$this->model_restaurant->del($id);
			redirect(base_url()."admin/restaurants/index");
		}
		public function createThumbnail($fileName,$path,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
			$config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}